<?php
namespace App\Api;

use PhalApi\Api;
use App\Domain\Area as DomainArea;
use PhalApi\Exception\BadRequestException;

/**
 * 区域,地址信息接口
 */
class Area extends Api {
    public function getRules() {
        return array(
            'index' => array(
                'area_id' => array('name' => 'area_id', 'require' => false, 'desc' => '城市id'),
            ),
            'addressList' => array(
                'user_id' => array('name' => 'user_id', 'require' => true, 'desc' => '用户id'),
            ),
            'insertAddress' => array(
                'user_id' => array('name' => 'user_id', 'require' => true, 'desc' => '用户id'),
                'consignee' => array('name' => 'consignee', 'require' => true, 'desc' => '收货人'),
                'province' => array('name' => 'province', 'require' => true, 'desc' => '省份id'),
                'city' => array('name' => 'city', 'require' => true, 'desc' => '城市id'),
                'district' => array('name' => 'district', 'require' => true, 'desc' => '区id'),
                'address' => array('name' => 'address', 'require' => true, 'desc' => '详细地址'),
                'mobile' => array('name' => 'mobile', 'require' => true, 'desc' => '手机号码'),
                'is_default' => array('name' => 'is_default', 'require' => true, 'desc' => '是否默认 1：默认  0：非默认'),
            ),
            'editAddress' => array(
                'user_id' => array('name' => 'user_id', 'require' => true, 'desc' => '用户id'),
                'address_id' => array('name' => 'address_id', 'require' => true, 'desc' => '地址id'),
                'consignee' => array('name' => 'consignee', 'require' => true, 'desc' => '收货人'),
                'province' => array('name' => 'province', 'require' => true, 'desc' => '省份id'),
                'city' => array('name' => 'city', 'require' => true, 'desc' => '城市id'),
                'district' => array('name' => 'district', 'require' => true, 'desc' => '区id'),
                'address' => array('name' => 'address', 'require' => true, 'desc' => '详细地址'),
                'mobile' => array('name' => 'mobile', 'require' => true, 'desc' => '手机号码'),
                'is_default' => array('name' => 'is_default', 'require' => true, 'desc' => '是否默认 1：默认  0：非默认'),
            ),
            'addressDel' => array(
                'user_id' => array('name' => 'user_id', 'require' => true, 'desc' => '用户id'),
                'address_id' => array('name' => 'address_id', 'require' => true, 'desc' => '地址id'),
            ),
        );
    }
    /**
     * 区域列表接口
     * @desc 获取区域列表
     * @return int id 区域id
     * @return int name 分类id
     */
    public function index() {
        $condition = array();
        if( isset( $this->area_id ) && $this->area_id > 0 ){
            $condition['parent_id'] = $this->area_id;
        }else{
            $condition['level'] = 1;
        }
        $domain = new DomainArea();
        $data = $domain -> getArea( $condition );
        if( empty( $data ) ){
            throw new BadRequestException('数据查询失败', 1);
        }
        return $data;
    }
    /**
     * 收货地址列表接口
     * @desc 获取区域列表
     * @return int address_id 收货地址id
     * @return string consignee 收货人
     * @return int province 省份id
     * @return int city 城市id
     * @return int district 区id
     * @return string province_name 省份名称
     * @return string city_name 城市名称
     * @return string district_name 区名称
     * @return string address 详细地址
     * @return int mobile 电话号码
     * @return int is_default 是否默认 1为默认
     */
    public function addressList() {
        $domain = new DomainArea();
        $data = $domain -> getAddressList( $this->user_id );
        if( empty( $data ) ){
            $data = array();
        }
        return $data;
    }
    /**
     * 添加收货地址接口
     * @desc 添加收货地址
     */
    public function insertAddress() {
        $data = array();
        $data['user_id'] = $this->user_id;
        $data['consignee'] = $this->consignee;
        $data['province'] = $this->province;
        $data['city'] = $this->city;
        $data['district'] = $this->district;
        $data['address'] = $this->address;
        $data['mobile'] = $this->mobile;
        $data['is_default'] = $this->is_default;
        $data['add_time'] = time();
        $domain = new DomainArea();
        $res = $domain -> insertAddress( $data );
        if( empty( $res ) ){
            throw new BadRequestException('数据添加失败', 1);
        }
        return $res;
    }

    /**
     * 编辑收货地址接口
     * @desc 添加收货地址
     */
    public function editAddress() {
        $data = array();
        $data['consignee'] = $this->consignee;
        $data['province'] = $this->province;
        $data['city'] = $this->city;
        $data['district'] = $this->district;
        $data['address'] = $this->address;
        $data['mobile'] = $this->mobile;
        $data['is_default'] = $this->is_default;
        $data['update_time'] = time();
        $domain = new DomainArea();
        $res = $domain -> editAddress( $this->user_id, $this->address_id, $data );
        if( empty( $res ) ){
            throw new BadRequestException('数据修改失败', 1);
        }
        return $res;
    }

    /**
     * 收货地址删除接口
     * @desc 收货地址删除
     */
    public function addressDel() {
        $domain = new DomainArea();
        $res = $domain -> addressDel( $this->user_id, $this->address_id );
        if( empty( $res ) ){
            throw new BadRequestException('数据删除失败', 1);
        }
        return $res;
    }

}
